#include "StdAfx.h"
#include "CameraManager.h"
//#include "LuaInterface.h"
//
//// lib
//#ifdef _DEBUG
//#pragma comment(lib, "../bin/debug/LuaInterface.lib")
//
//#else
//#pragma comment(lib, "../bin/release/LuaInterface.lib")
//
//#endif

using namespace Ogre;
CameraManager::CameraManager(void)
:	m_pCam(NULL), 
m_bLeftPressed(false), m_bRightPressed(false),
m_bMoveRight(false), m_bMoveLeft(false),
m_bMoveUp(false), m_bMoveDown(false),
m_bMoveForward(false), m_bMoveBack(false),
m_fMoveSpeed(100)
{

}

CameraManager::~CameraManager(void)
{
}

bool CameraManager::frameStarted(const Ogre::FrameEvent& evt)
{
	//LuaInterface lua;
	//lua.loadModule("ModelView.lua");
	Ogre::Vector3 move_offset = Ogre::Vector3::ZERO;
	if(m_bMoveRight) move_offset += m_pCam->getRight();
	if(m_bMoveLeft) move_offset -= m_pCam->getRight();
	if(m_bMoveUp) move_offset += m_pCam->getUp();
	if(m_bMoveDown) move_offset -= m_pCam->getUp();
	if(m_bMoveForward) move_offset += m_pCam->getDirection();
	if(m_bMoveBack) move_offset -= m_pCam->getDirection();
	m_pCam->move(move_offset * m_fMoveSpeed * evt.timeSinceLastFrame);

	return true;
}

bool CameraManager::mouseMoved(const OIS::MouseEvent &arg)
{
	float scale = 0.002f;
	if(m_bRightPressed && m_pCam)
	{
		m_pCam->yaw(Ogre::Radian(-arg.state.X.rel * scale));
		m_pCam->pitch(Ogre::Radian(-arg.state.Y.rel * scale));
	}
	if(arg.state.Z.rel != 0)
	{
		Ogre::Vector3 cam_dir = m_pCam->getDirection().normalisedCopy();
		m_pCam->move( cam_dir * arg.state.Z.rel / 4 );
	}
	return true;
}

bool CameraManager::mousePressed(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
{
	this->setMouseState(id, true);
	return true;
}

bool CameraManager::mouseReleased(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
{
	this->setMouseState(id, false);	
	return true;
}

bool CameraManager::keyPressed(const OIS::KeyEvent& e)
{
	this->setKeyState(e, true);
	return true;
}

bool CameraManager::keyReleased(const OIS::KeyEvent& e)
{
	this->setKeyState(e, false);
	return true;
}

void CameraManager::setCamera(Ogre::Camera* pCam)
{
	m_pCam = pCam;
}

void CameraManager::setCameraMode(Mode mode)
{
	m_mode = mode;
}

void CameraManager::setMouseState(OIS::MouseButtonID id, bool isPressed)
{
	if(id == OIS::MB_Left)
		m_bLeftPressed = isPressed;
	if(id == OIS::MB_Right)
		m_bRightPressed = isPressed;
}

void CameraManager::setKeyState(const OIS::KeyEvent& e, bool isPressed)
{
	if(e.key == OIS::KC_D || e.key == OIS::KC_RIGHT)
		m_bMoveRight = isPressed;
	if(e.key == OIS::KC_A || e.key == OIS::KC_LEFT)
		m_bMoveLeft = isPressed;
	if(e.key == OIS::KC_W || e.key == OIS::KC_UP)
		m_bMoveUp = isPressed;
	if(e.key == OIS::KC_S || e.key == OIS::KC_DOWN)
		m_bMoveDown = isPressed;
	if(e.key == OIS::KC_ADD)
		m_fMoveSpeed += 10;
	if(e.key == OIS::KC_SUBTRACT)
		m_fMoveSpeed -= 10;
}